﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;

namespace DAL
{
    public class CalleDAL : BaseDAL
    {
        public List<CalleBE> SeleccionarPorParametros(BaseSC pFiltroSC, int pPageSize, int? pPageNum, ref int pCountReg)
        {
            CalleSC wFiltroSC = (CalleSC)pFiltroSC;
            if (wFiltroSC == null)
                wFiltroSC = new CalleSC();

            List<CalleBE> listaDeCalleBE = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdCalle", wFiltroSC.IdCalle));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", wFiltroSC.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@pagesize", pPageSize));
                cmd.Parameters.Add(new SqlParameter("@pagenum", pPageNum));

                SqlParameter p = new SqlParameter();
                p.ParameterName = "@PageCount";
                p.Direction = ParameterDirection.Output;
                p.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(p);

                cmd.CommandText = "Calles_GetAllPaginated";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    Int32 idCalleIndex = reader.GetOrdinal("IdCalle");
                    Int32 descripcionIndex = reader.GetOrdinal("Descripcion");

                    listaDeCalleBE = new List<CalleBE>();

                    while (reader.Read())
                    {
                        CalleBE oCalleBE = new CalleBE();
                        Object[] values = new Object[reader.FieldCount];
                        reader.GetValues(values);

                        oCalleBE.IdCalle = Convert.ToInt32(values[idCalleIndex]);
                        oCalleBE.Descripcion = Convert.ToString(values[descripcionIndex]);

                        listaDeCalleBE.Add(oCalleBE);
                    }
                }
                reader.Close();
                pCountReg = (Int32)cmd.Parameters["@PageCount"].Value;

            }
            return listaDeCalleBE;
        }

        public CalleBE SeleccionarPorId(CalleBE oCalleBE)
        {
            CalleBE oCalle = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdCalle", oCalleBE.IdCalle));
                cmd.CommandText = "Calles_GetById";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        oCalle = ConstruirCalle(reader);
                    }
                }

            }
            return oCalle;
        }

        private CalleBE ConstruirCalle(SqlDataReader reader)
        {
            CalleBE oCalleBE = new CalleBE();
            Object[] values = new Object[reader.FieldCount];

            reader.GetValues(values);

            oCalleBE.IdCalle = Int32.Parse(reader["IdCalle"].ToString());
            oCalleBE.Descripcion = reader["Descripcion"].ToString();

            return oCalleBE;
        }

        public void Insertar(CalleBE Calle)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Calles_i";
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", Calle.Descripcion));

                    SqlParameter p = new SqlParameter();
                    p.ParameterName = "@IdCalle";
                    p.Direction = ParameterDirection.Output;
                    p.SqlDbType = SqlDbType.Int;
                    cmd.Parameters.Add(p);

                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    Calle.IdCalle = (Int32)cmd.Parameters["@IdCalle"].Value;
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Modificar(CalleBE pCalle)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Calles_u";
                    cmd.Parameters.Add(new SqlParameter("@IdCalle", pCalle.IdCalle));
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pCalle.Descripcion));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }
        }

        public void Delete(CalleBE pCalle)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Calles_d";
                    cmd.Parameters.Add(new SqlParameter("@IdCalle", pCalle.IdCalle));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }
        }
    }
}
